package com.kiln.juxingqs.ui

import android.content.Intent
import android.content.IntentFilter
import android.view.View
import android.view.ViewGroup
import client.global.commlib.base.SyBannerAdapter
import client.global.commlib.getScreenWidth
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.bumptech.glide.request.RequestOptions
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.api.bean.GoodsIndex
import com.kiln.juxingqs.api.bean.ShopingBean
import com.kiln.juxingqs.api.bean.TradeGoodsDetail
import com.kiln.juxingqs.databinding.ActivityLotDealBinding
import com.kiln.juxingqs.databinding.ActivityLotDealstatus1Binding
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.receiver.ZrGDListBroadcast
import com.kiln.juxingqs.ui.vm.LotDealModel
import com.youth.banner.indicator.CircleIndicator
import java.util.ArrayList

/**
 * 商品购买
 */
class LotDealStatus1Activity : BaseMiddleActivity<LotDealModel, ActivityLotDealstatus1Binding>() {

    lateinit var bannerList: List<GoodsDetail.Banner>
    lateinit var detailList: List<GoodsDetail.Detail>
    var tGoodsDetail: TradeGoodsDetail? = null
    var goods_id: String? = ""

    override fun getLayout(): Int {
        return R.layout.activity_lot_dealstatus1
    }

    override fun initViewModel(): LotDealModel = createViewModel()

    val meBroadcast: ZrGDListBroadcast by lazy {
        ZrGDListBroadcast()
    }

    override fun initView() {
        binding.vm = viewModel
        binding.event = this
        binding.refreshLayout.setOnRefreshListener {
            getData()
        }
        binding.emptyLayout.setOnClickListener {
            getData()
        }

        registerReceiver(meBroadcast, IntentFilter(ZrGDListBroadcast.REFREASH_GUADAN_LIST))

//        bannerList = intent.getParcelableArrayListExtra<GoodsDetail.Banner>("banner_list")!!
//        detailList = intent.getParcelableArrayListExtra<GoodsDetail.Detail>("detail_list")!!
        goods_id = intent.getStringExtra("goods_id")!!


    }


    override fun onResume() {
        super.onResume()
        getData()

    }

    override fun observerUI() {
        viewModel.callUser.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    binding.userInfo = it.data

                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
//                binding.refreshLayout.isRefreshing = false
            }

        })

        viewModel.tradeGoodsDetail.observe(this, {
            it?.let {
                if (it.isSuccess && it.data != null) {
                    binding.emptyLayout.visibility = View.GONE
                    binding.bean = it.data
                    tGoodsDetail = it.data
                    bannerList = it.data!!.banner_list as ArrayList<GoodsDetail.Banner>
                    detailList = it.data!!.detail_list as ArrayList<GoodsDetail.Detail>


                    if (bannerList != null && bannerList.size > 0) {
                        val width = getScreenWidth(this)
                        val params: ViewGroup.LayoutParams = binding.banner.getLayoutParams()
                        params.height = width //(width * 575 / 1400f).toInt()
                        binding.banner.setLayoutParams(params)
                        binding.banner.setIndicator(CircleIndicator(this))

                        val list = bannerList
                        binding.banner.setAdapter(object :
                            SyBannerAdapter<GoodsDetail.Banner>(list) {
                            override fun onBindView(
                                holder: BannerViewHolder?,
                                data: GoodsDetail.Banner?,
                                position: Int,
                                size: Int
                            ) {

                                mImgLoader.load(data?.goods_img)
                                    .apply(RequestOptions().error(R.mipmap.ic_launcher)).into(
                                        holder!!.imageView
                                    )
                            }
                        })
                        binding.banner.start()
                    }

                    it.message?.let {
                        toast(it)
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.buyRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast(it.message)
                    getData()
                }
            }
        })
    }

    private fun getData() {
        viewModel.userInfo()
        viewModel.holdGoodsDetailCall(goods_id!!)

    }


    //查看商品详情
    fun goDetail() {
        if (!bannerList.isNullOrEmpty() || !detailList.isNullOrEmpty()) {
            startActivity(Intent(this, PpZrDetailActivity::class.java).apply {
                putExtra("banner_list", bannerList as ArrayList<GoodsDetail.Banner>)
                putExtra("detail_list", detailList as ArrayList<GoodsDetail.Detail>)
            })
        }

    }

    //当前价购买 / 特价购买
    fun goDifTypeBuy(type: Int) {
        when (type) {
            1 -> {//特价购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
                            viewModel.holdBuyTj(it, num)
                            dismiss()
                        }
                    }.show()
                }
            }

            2 -> {//优惠购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
                            viewModel.holdBuyYh(it, num)
                            dismiss()
                        }
                    }.show()
                }
            }

            else -> {//当前购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
//                            viewModel.holdBuyYj(it, num)
                            dismiss()

                            startActivity(
                                Intent(
                                    this@LotDealStatus1Activity,
                                    ConfirmOrderActivity::class.java
                                ).apply {
                                    putExtra("bean", ShopingBean(
                                        goods_id = "${it.goods_dict.goods_id}",
                                        name = "${it.goods_dict.name}",
                                        price = "${it.goods_dict.price}",
                                    ))
                                    putExtra("num", num)
                                })
                        }
                    }.show()
                }

            }
        }

        /* startActivity(Intent(this, XyzrNewActivity::class.java).apply {
             putExtra("index", 1)
             putExtra("titleName", "")

         })*/

    }


    //特价券 明细
    fun goLots1(num: String) {
//            startActivity(Intent(this, XyzrNewActivity::class.java).apply {
//                putExtra("detail_type", "20")//r_label还不确定
//            })

        tGoodsDetail?.goods_dict?.let {
        }
    }

    //优惠券 明细
    fun goLots2(num: String) {
        tGoodsDetail?.goods_dict?.let {
//            startActivity(Intent(this, XyzrNewActivity::class.java).apply {
//                putExtra("detail_type", "20")//r_label还不确定
//            })

        }
    }


    //通用明细
    fun goZrLots(num: String, pick_type: String, title_name: String, r_label: Int) {
        binding.bean?.let {
            startActivity(Intent(this, ZrLotsActivity::class.java).apply {
                putExtra("goods_id", goods_id)
                putExtra("goods_name", it.goods_dict.name)
                putExtra("banner_list", bannerList as ArrayList<GoodsDetail.Banner>)
                putExtra("detail_list", detailList as ArrayList<GoodsDetail.Detail>)
                putExtra("deal_num", num)
                putExtra("pick_type", pick_type)
                putExtra("title_name", title_name)
                putExtra("r_label", r_label)

//                //预支点对点
//                putExtra("goods_price", it.price)
//                //资产二
//                putExtra("price_pf", it.price_pf)
//                //可交易商品
//                putExtra("f_price", it.f_price)
//                putExtra("g_price", it.g_price)


            })
        }

    }

    fun goth() {
        startActivity(Intent(this, WdthActivity::class.java))
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(meBroadcast)
    }

}